#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：auto-test 
@File    ：GoodsPage.py
@Author  ：赵加响
@Date    ：2022/12/16 18:11 
@Desc    ：
"""
from time import sleep

from selenium.webdriver.common.by import By

from base.GoodsBase import GoodsBase
from base.ObjectMap import ObjectMap
from common.tools import get_image_path


class GoodsPage(GoodsBase, ObjectMap):

    def input_goods_title(self, driver, input_value):
        """
        输入商品标题
        :param driver:
        :param input_value:
        :return:
        """
        goods_title_xpath = self.goods_title()
        return self.element_fill_value(driver, By.XPATH, goods_title_xpath, input_value)

    def input_goods_details(self, driver, input_value):
        """
        输入商品详情
        :param driver:
        :param input_value:
        :return:
        """
        goods_detail_xpath = self.goods_details()
        return self.element_fill_value(driver, By.XPATH, goods_detail_xpath, input_value)

    def select_goods_num(self, driver, num):
        """
        输入商品数量
        :param driver:
        :param num:
        :return:
        """
        goods_num_add_xpath = self.goods_num()
        for i in range(num):
            self.element_click(driver, By.XPATH, goods_num_add_xpath)
            sleep(0.5)

    def upload_goods_image(self, driver, image_name):
        """
        上传商品图片
        :param driver:
        :param image_name:
        :return:
        """
        image_path = get_image_path(image_name)
        upload_xpath = self.goods_image()
        return self.upload(driver, By.XPATH, upload_xpath, image_path)

    def input_goods_price(self, driver, input_value):
        """
        输入商品价格
        :param driver:
        :param input_value:
        :return:
        """
        goods_price_xpath = self.goods_price()
        return self.element_fill_value(driver, By.XPATH, goods_price_xpath, input_value)

    def select_goods_status(self, driver, select_name):
        """
        选择商品的状态
        :param driver:
        :param select_name:
        :return:
        """
        select_status_xpath = self.goods_status()
        self.element_click(driver, By.XPATH, select_status_xpath)
        sleep(0.5)
        goods_select_status_status = self.goods_status_select(select_name)
        return self.element_click(driver, By.XPATH, goods_select_status_status)

    def click_bottom_button(self, driver, button_name):
        """
        点击底部按钮
        :param driver:
        :param button_name:
        :return:
        """

        button_xpath = self.add_goods_bottom_button(button_name)
        return self.element_click(driver, By.XPATH, button_xpath)

    def add_new_goods(self, driver, goods_title, goods_detail, goods_num, goods_pic_list, goods_price, goods_status,
                      bottom_button_name):
        """
        新加goods
        :param driver:
        :param goods_title:
        :param goods_detail:
        :param goods_num:
        :param goods_pic_list:
        :param goods_price:
        :param goods_status:
        :param bottom_button_name:
        :return:
        """
        self.input_goods_title(driver, goods_title)
        self.input_goods_details(driver, goods_detail)
        self.select_goods_num(driver, goods_num)
        for goods_pic in goods_pic_list:
            self.upload_goods_image(driver, goods_pic)
            sleep(2)
        self.input_goods_price(driver, goods_price)
        self.select_goods_status(driver, goods_status)
        self.click_bottom_button(driver, bottom_button_name)
